package com.api.phm.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.api.phm.domain.query.score.PhmLineScoreQuery;
import com.api.phm.domain.vo.PhmDevicesScoreVo;
import com.api.phm.mapper.PhmDevicesScoreMapper;
import com.api.phm.service.IPhmDevicesScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PhmDevicesScoreServiceImpl implements IPhmDevicesScoreService {
    @Autowired
    private PhmDevicesScoreMapper phmDevicesScoreMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<PhmLineScoreQuery> searchDevicesScore(PhmDevicesScoreVo vo) {
        return phmDevicesScoreMapper.searchDevicesScore(vo);
    }

    @Override
    public List<Map<String, Object>> evlAvgScoreOfSlope(PhmDevicesScoreVo vo) {
        Query query = new Query();
        if (vo.getStartTime() != null && vo.getEndTime() != null) {
            query.addCriteria(
                    new Criteria().andOperator(
                            Criteria.where("dateTime").lte(vo.getStartTime()),
                            Criteria.where("dateTime").gte(vo.getEndTime())
                    ));
        } else if (vo.getStartTime() != null) {
            query.addCriteria(
                    new Criteria().andOperator(
                            Criteria.where("dateTime").lte(vo.getStartTime())
                    ));
        } else if (vo.getEndTime() != null) {
            query.addCriteria(
                    new Criteria().andOperator(
                            Criteria.where("dateTime").gte(vo.getEndTime())
                    ));
        }
        if (vo.getPlcId() != null) {
            query.addCriteria(Criteria.where("plcId").is(vo.getPlcId()));
        }
        if (vo.getRobotId() != null) {
            query.addCriteria(Criteria.where("robotId").is(vo.getRobotId()));
        }
        if (vo.getElementId() != null) {
            query.addCriteria(Criteria.where("elementId").is(vo.getElementId()));
        }
        if (vo.getDatCol() != null) {
            query.addCriteria(Criteria.where("datCol").is(vo.getDatCol()));
        }
//        mongoTemplate.;
        List<JSONObject> list = mongoTemplate.find(query, JSONObject.class, "tracing_list");
        List<Map<String, Object>> rList = new ArrayList<>();
        list.stream().forEach(i -> {
            Map<String, Object> map = new HashMap<>();
            map.put("date", i.getString("dateTime"));
            JSONObject tracingInfo = JSONObject.from(i.get("tracingInfo"));
            map.put("score_of_slope", tracingInfo.getDoubleValue("score"));
            rList.add(map);
        });
        return rList;
    }

    @Override
    public Map<String, Object> actionEstimat(PhmDevicesScoreVo vo) {
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> rList = new ArrayList<>();
        Query query = new Query();
        query.addCriteria(Criteria.where("robotId").is(vo.getRobotId()));
        query.addCriteria(Criteria.where("elementId").is(vo.getElementId()));
        query.addCriteria(Criteria.where("datCol").is(vo.getDatCol()));
        query.addCriteria(
                new Criteria().andOperator(
                        Criteria.where("dateTime").gte(vo.getStartTime()),
                        Criteria.where("dateTime").lte(vo.getEndTime())
                ));
        List<JSONObject> list = mongoTemplate.find(query, JSONObject.class, "takt_list");
        List<Double> doubleList = new ArrayList<>();
        list.stream().forEach(i -> {
            Map<String, Object> map = new HashMap<>();
            map.put("time", i.getString("dateTime"));
            JSONObject taskInfo = JSONObject.from(i.get("taskData"));
            map.put("value", taskInfo.getDoubleValue("evl1"));
            doubleList.add(taskInfo.getDoubleValue("evl1"));
            rList.add(map);
        });
        Double sumDouble = rList.stream().mapToDouble(e -> (double) e.get("value")).sum();
        int count = list.size();
        Double avg = (double) 0;
        if (count > 0) {
            avg = sumDouble / count;
        }
        data.put("data", rList);
        data.put("avg", avg);
        return data;
    }
}
